import { _decorator, Component, Label, Node } from 'cc';
import EventExt from 'db://cocos-ext/base/EventExt';
import ScrollLabel from 'db://cocos-ext/component/ScrollLabel';
import { games, rooms, yy } from 'yycore';

import SlotBetModule from './SlotBetModule';
import { SLOT_WIN_LOSE_CLEAR, SLOT_WIN_LOSE_MONEY_CNT } from './SlotEventType';

const { ccclass, property } = _decorator;

@ccclass('SlotBottomBar')
export default class SlotBottomBar extends Component {

    // 输赢文本
    @property(Label)
    private readonly winMoneyLabel: Label = null;

    // 输赢钱
    private _winLoseMoney: number = 0;
    // 输赢钱动画组件
    private winLabalAni: ScrollLabel = null;
    // 输赢钱文本格式化
    private getFormatStr(coinCount: number): string {
        return coinCount > 0 ? games.gf.formatCoin({ coinCount, scale: false }) : "";
    }

    onLoad() {
        this.initWinLoseTxt();
    }

    onDestroy() {
        SlotBetModule.winLoseNode = null;
        EventExt.targetOff(this);
    }

    public start() {
        if (rooms.service.isLogin())
            this.onEnterRoomResult();
        else
            yy.event.on(this, games.events.EnterRoomResult, this.onEnterRoomResult);
    }

    // 进房成功后，初始化房间配置信息
    private onEnterRoomResult() {
        SlotBetModule.lockChips;
        SlotBetModule.betChips;
    }

    private initWinLoseTxt() {
        SlotBetModule.winLoseNode = this.winMoneyLabel.node;
        // 文本滚动动画组件
        this.winLabalAni = ScrollLabel.create(this.winMoneyLabel, {
            formatFunc: this.getFormatStr.bind(this),
            value: 0
        });
        // 监听输赢钱数量事件
        EventExt.on(SLOT_WIN_LOSE_MONEY_CNT, (v: number) => {
            const toMoney = this._winLoseMoney + v;
            if (v > 0) {
                this.winLabalAni.scrollTo(toMoney);
                this._winLoseMoney = toMoney;
            } else {
                this.winLabalAni.value = toMoney;
            }
        }, this);
        // 监听清空输赢文本事件
        EventExt.on(SLOT_WIN_LOSE_CLEAR, () => {
            this._winLoseMoney = 0;
            this.winMoneyLabel.string = "";
        }, this);
    }


}
